// 封装axios请求
import axios from "axios";

const instance = axios.create({
  baseURL: "http://localhost:3000/api",
  timeout: 5000,
});
// 设置请求拦截器
instance.interceptors.request.use(
  (config) => {
    const token = localStorage.getItem("token");
    if (token) {
      config.headers.Authorization = `Bearer ${token}`;
    }
    return config;
  },
  (error) => {
    return Promise.reject(error);
  }
);
// 响应拦截
instance.interceptors.response.use(
    (res) => {
      //console.log(res);
      if (res.status === 200) {
        return res.data;
      } else {
        console.log("请求失败");
        return Promise.reject(res.data);
      }
    },
    (err) => {
      console.log("请求超时");
      return Promise.reject(err);
    }
  );
export default instance;
